package com.lockdemo;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class MyService2{
	private Lock lock = new ReentrantLock();
	public void methodA(){
		try {
			lock.lock();
			System.out.println("methodA begin ThreadName="+Thread.currentThread().getName()+", time="+System.currentTimeMillis());
			Thread.sleep(5000);
			System.out.println("methodA end   ThreadName="+Thread.currentThread().getName()+", time="+System.currentTimeMillis());
		} catch (Exception e) {
		}finally{
			lock.unlock();
		}
	}
	public void methodB(){
		try {
			lock.lock();
			System.out.println("methodB begin ThreadName="+Thread.currentThread().getName()+", time="+System.currentTimeMillis());
			Thread.sleep(5000);
			System.out.println("methodB end   ThreadName="+Thread.currentThread().getName()+", time="+System.currentTimeMillis());
		} catch (Exception e) {
		}finally{
			lock.unlock();
		}
	}
}
class ThreadA extends Thread{
	private MyService2 myService2;

	public ThreadA(MyService2 myService2) {
		super();
		this.myService2 = myService2;
	}
	@Override
	public void run() {
		myService2.methodA();
	}
}
class ThreadAA extends Thread{
	private MyService2 myService2;

	public ThreadAA(MyService2 myService2) {
		super();
		this.myService2 = myService2;
	}
	@Override
	public void run() {
		myService2.methodA();
	}
}
class ThreadB extends Thread{
	private MyService2 myService2;

	public ThreadB(MyService2 myService2) {
		super();
		this.myService2 = myService2;
	}
	@Override
	public void run() {
		myService2.methodB();
	}
}
class ThreadBB extends Thread{
	private MyService2 myService2;

	public ThreadBB(MyService2 myService2) {
		super();
		this.myService2 = myService2;
	}
	@Override
	public void run() {
		myService2.methodB();
	}
}
public class Run2 {

	public static void main(String[] args) {
		MyService2 myService2=new MyService2();
		ThreadA a = new ThreadA(myService2);
		a.setName("A"); a.start();
		ThreadAA aa = new ThreadAA(myService2);
		aa.setName("AA"); aa.start();
		
		ThreadB b = new ThreadB(myService2);
		b.setName("B"); b.start();
		ThreadBB bb = new ThreadBB(myService2);
		bb.setName("BB"); bb.start();
	}

}
